<?php
  require_once('../db.php');

  if(isset($_GET['mode']) &&
  $_GET['mode'] == 'change_status' ) {
    $cStatus = $_GET['current_status'];
    $nStatus = $cStatus==0 ? 1:0;
    $id = $_GET['id'];

$data = array('status'=>$nStatus);
$affectedRows = update('users',"`id`='$id'",$data);

    if($affectedRows == 1){
      echo "Status Updated successfully.";
    }
  }

  if(isset($_GET['mode']) &&
  $_GET['mode'] == 'del' ) {
    $id = $_GET['id'];
    $deletedRows = delete('users', "`id`='$id'");
    if($deletedRows == 1){
      echo "<h4>Record Deleted successfully.</h4>";
    }
  }

  $search = $where = '';

  $order = isset($_GET['order'])?$_GET['order']:'asc';

  $newOrder = $order=='asc'?'desc':'asc';

  $fieldName = isset($_GET['field'])?$_GET['field']:'added_date';


  $orderBy = "`$fieldName` $order";


  if(isset($_GET['search']) && !empty($_GET['search'])) {
    $search = strClean($_GET['search']);
    $where = "`uname` LIKE '%$search%' OR `email` LIKE '%$search%'";
  }

  $totalUsers = countAll('users',$where);

  $rpp = 3; # Offset
  $totalPages = ceil($totalUsers / $rpp);

  $c_page = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 1;

  $limit = ($c_page - 1) * $rpp;

$result = fetchAll('users',$where,'',$orderBy,"$limit,$rpp");

?>
<p>
  <em>Total Records </em>:<strong><?=$totalUsers?></strong>
</p>
<form action="" method="get">
  <input type="search" value="<?=$search?>" name="search" placeholder="Search Records" />
  <input type="submit" value="Search" />
</form>
<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <th colspan="6">:: ALL USERS ::</th>
    </tr>
    <tr>
      <th align="left">#</th>
      <th align="left">
<a href="users.php?page=<?=$c_page?>&search=<?=$search?>&field=uname&order=<?=$newOrder?>">
        User Name
</a>
        </th>
      <th align="left">Email</th>
      <th align="left">Avatar</th>
      <th align="left">Added date</th>
      <th align="left">Status</th>
      <th align="left">Action</th>
    </tr>
  </thead>
  <tbody>
<?php
  while($ans = mysql_fetch_assoc($result))
  {
    $id = $ans['id'];
    pr($ans);
?>
    <tr>
      <td ><?=$id?></td>
      <td ><?=$ans['uname']?></td>
      <td >

<a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a>
      </td>

      <td >

<a target="_blank" href="../uploads/<?=@$ans['avatar']?>"><img src="../uploads/thumb/<?=@$ans['avatar']?>"></a>
      </td>


      <td ><?=date('d-M-Y',strtotime($ans['added_date']))?></td>
      <td ><a href="users.php?mode=change_status&current_status=<?=$ans['status']?>&id=<?=$id?>"><?=getStatus($ans['status'])?></a></td>
      <td align="right">

        <a target="_blank" href="edit_user.php?id=<?=$id?>">Edit</a>


         / <a href="users.php?mode=del&id=<?=$id?>" onClick="return confirm('Are you sure ?')">Delete</a></td>
    </tr>
<?php
  }
?>
  </tbody>
</table>

<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <tr>
<?php
for ($i = 1; $i <= $totalPages ; $i++) {
  if($c_page == $i){
?>

      <th><span style="color:red;font-size:25px;"><?=$i?></span></th>
<?php
  } else {
    $txtsearch=($search=='')?'':"&search=$search";
?>
 <th><a href="users.php?page=<?=$i?><?=$txtsearch?>"><?=$i?></a></th>
<?php
  }
}// end for
?>
    </tr>
</table>
<?php
  require_once('footer.php');
?>
